﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadExpress.Models;
using LeadExpress.Models.ViewPages;
using LeadExpress.Utility;

namespace LeadExpress.Repository
{
    public class SystemSettingManagementRepository : BaseManagementRepository
    {
        LeadExpressEntities entities = new LeadExpressEntities();
        
        public SystemSettingManagementModel GetModel(string pageType, string roleName, string filePath)
        {
            SystemSettingManagementModel model = new SystemSettingManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.WebsiteSettingInfo = GetSettingInfo(filePath);

            return model;
        }

        public void  SaveSettingInfo(string filePath, WebsiteSettingConfig model)
        {
            Utility.LeadTools.Serializer(filePath, model);
        }

        private WebsiteSettingConfig GetSettingInfo(string filePath)
        {
            WebsiteSettingConfig model = new WebsiteSettingConfig();
            model = Utility.LeadTools.Deserialize(filePath, model);

            return model;
        }

        #region Generate to XML file
        public void GenerateXMLFromCategory(string filePath, ref bool success)
        {
            try
            {
                NavigationConfig config = new NavigationConfig();
                List<NavigationItems> navList = new List<NavigationItems>();

                // Build config object
                List<CategoryModel> catList = new CategoryRepository().GetAll();
                List<SubCategoryModel> subList = new SubCategoryRepository().GetAll();

                // Add item to list
                foreach (var item in catList)
                {
                    NavigationItems model = new NavigationItems();
                    model.ItemName = item.Name;
                    model.ItemLink = string.Format("/raovat/c{0}/{1}", item.ID, StringClearFormat.ClearCharacterSpecial(item.Name));
                    model.SubItems = BuildListLinkSubCategory(item.ID, subList.Where(c => c.CategoryId == item.ID).ToList());

                    navList.Add(model);
                } 

                // Assign list to config model
                config.NavigationItems = navList;

                // Write to xml file
                Utility.LeadTools.Serializer(filePath, config);
            }
            catch
            {
                success = false;
            }
        }

        private List<NavigationItem> BuildListLinkSubCategory(int categoryId, List<SubCategoryModel> subList)
        {
            List<NavigationItem> list = new List<NavigationItem>();

            if (subList != null && subList.Count > 0)
            {
                foreach (var item in subList)
                {
                    NavigationItem model = new NavigationItem();
                    model.ItemName = item.Name;
                    model.ItemLink = string.Format("/raovat/c{0}-s{1}/{2}", categoryId, item.ID, StringClearFormat.ClearCharacterSpecial(item.Name));
                    list.Add(model);
                }
            }   

            return list;
        }

        public void GenerateXMLFromState(string filePath, ref bool success)
        {
            try
            {
                NavigationConfig config = new NavigationConfig();
                List<NavigationItems> navList = new List<NavigationItems>();

                // Build config object
                List<StateModel> stateList = new StateRepository().GetAll();
                List<DistrictModel> districtList = new DistrictRepository().GetAll();

                // Add item to list
                foreach (var item in stateList)
                {
                    NavigationItems model = new NavigationItems();
                    model.ItemName = item.Name;
                    model.ItemLink = string.Format("/raovat/{0}/{1}", item.ID.ToLower(), StringClearFormat.ClearCharacterSpecial(item.Name));
                    model.IsTop = item.IsTop;
                    model.SubItems = BuildListLinkDistrict(item.ID, districtList.Where(c => c.StateId == item.ID).ToList());

                    navList.Add(model);
                } 

                // Assign list to config model
                config.NavigationItems = navList;

                // Write to xml file
                Utility.LeadTools.Serializer(filePath, config);
            }
            catch
            {
                success = false;
            }
        }

        private List<NavigationItem> BuildListLinkDistrict(string stateId, List<DistrictModel> districtList)
        {
            List<NavigationItem> list = new List<NavigationItem>();

            if (districtList != null && districtList.Count > 0)
            {
                foreach (var item in districtList)
                {
                    NavigationItem model = new NavigationItem();
                    model.ItemName = item.Name;
                    model.ItemLink = string.Format("/raovat/{0}-d{1}/{2}", stateId.ToLower(), item.ID, StringClearFormat.ClearCharacterSpecial(item.Name));
                    list.Add(model);
                }
            }   

            return list;
        }
        #endregion
    }
}